//
//  logDelegate.m
//  TcpTest_Server
//
//  Created by scarlettsun on 12-7-27.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "MiniSocket.h"
#import "SockPacket.h"
#import "MainSocketDelegate.h"

@implementation MiniSocket
@synthesize miniAsynSocket;
@synthesize userId;
@synthesize listenPort;

-(void) dealloc
{
    buffer = nil;
    self.miniAsynSocket = nil;
    //[super dealloc];
}

-(MiniSocket *) initWithSock:(GCDAsyncSocket *)sock
{
    self.miniAsynSocket = sock ;
    self.miniAsynSocket.delegate = self;
    packetLength = 0;
    buffer = nil;
    readMode = Readlenth;
    packetDispatchCenter = [[PacketDispatchCenter alloc] init];
    return self;
}

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket;
{}

//GCDAsyncSocket  not respond to this function 
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
{
    NSLog(@"mini socket  didConnectToHost\r\n"); 
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;
{
    NSLog(@"mini socket  didWriteDataWithTag\r\n");
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
{
    switch (readMode) {
        case Readlenth:
        {
            packetLength =*((NSUInteger *)([data bytes]));
            //NSLog(@"lenth is dd %lu",packetLength);
            [sock readDataToLength:packetLength withTimeout:-1 tag:0];
            readMode = ReadContent;
            break; 
        }
        case ReadContent:
        {  
            if ([data length] == packetLength) 
            { 
                readMode = Readlenth;
                /*
                 add packet to thePacketQueue          
                 */
                SockPacket * packet = [[SockPacket alloc] initSockPacket:packetLength packData:data sock:sock];
                @synchronized(packetDispatchCenter)
                {
                 [packetDispatchCenter doPacketDispatch:packet];
                }
            }
            [sock readDataToLength:LENGTH withTimeout:-1 tag:0];
            packetLength = 0;
            break;
        }
       default:
        { 
            [sock readDataToLength:LENGTH withTimeout:-1 tag:0];
            break;
        }
    }
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
{
    self.miniAsynSocket = nil;
    /* 
     remove  socket 
     */
    @synchronized([MainSocketDelegate connectedSocketsQueue])
    {
        
        for (int i = 0; i < [[MainSocketDelegate connectedSocketsQueue] count]; i++) 
        {
            MiniSocket *_sock  = [[MainSocketDelegate connectedSocketsQueue] objectAtIndex:i];
            if ([_sock.miniAsynSocket.connectedHost isNotEqualTo:self.miniAsynSocket.connectedHost]
                || _sock.miniAsynSocket.connectedPort !=self.miniAsynSocket.connectedPort) 
            {
                continue;
            }
            else
            {
                [[MainSocketDelegate connectedSocketsQueue] removeObject:_sock];
                return;
            }
        }
    }  
     
}

@end
